package com.ruoyi.system.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.Random;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.httpclient.httpclient.HttpClientHelper;
import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.system.domain.RongQiInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PhonesMapper;
import com.ruoyi.system.domain.Phones;
import com.ruoyi.system.service.IPhonesService;
import com.ruoyi.common.core.text.Convert;

/**
 * 机型库Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-12
 */
@Service
public class PhonesServiceImpl implements IPhonesService 
{
    @Autowired
    private PhonesMapper phonesMapper;

    @Autowired
    private  HttpClientHelper httpClientHelper;

    /**
     * 查询机型库
     * 
     * @param id 机型库主键
     * @return 机型库
     */
    @Override
    public Phones selectPhonesById(Long id)
    {
        return phonesMapper.selectPhonesById(id);
    }

    /**
     * 查询机型库列表
     * 
     * @param phones 机型库
     * @return 机型库
     */
    @Override
    public List<Phones> selectPhonesList(Phones phones)
    {
        return phonesMapper.selectPhonesList(phones);
    }

    /**
     * 新增机型库
     * 
     * @param phones 机型库
     * @return 结果
     */
    @Override
    public int insertPhones(Phones phones)
    {
        phones.setCreateTime(DateUtils.getNowDate());
        return phonesMapper.insertPhones(phones);
    }

    /**
     * 修改机型库
     * 
     * @param phones 机型库
     * @return 结果
     */
    @Override
    public int updatePhones(Phones phones)
    {
        phones.setUpdateTime(DateUtils.getNowDate());
        return phonesMapper.updatePhones(phones);
    }

    /**
     * 批量删除机型库
     * 
     * @param ids 需要删除的机型库主键
     * @return 结果
     */
    @Override
    public int deletePhonesByIds(String ids)
    {
        return phonesMapper.deletePhonesByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除机型库信息
     * 
     * @param id 机型库主键
     * @return 结果
     */
    @Override
    public int deletePhonesById(Long id)
    {
        return phonesMapper.deletePhonesById(id);
    }

    @Override
    public String importPhone(List<Phones> phoneList, Boolean isUpdateSupport, String operName) {

        if (StringUtils.isNull(phoneList) || phoneList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();


            try {
                // 验证是否存在这个用户
                boolean phoneFlag = false;
                for (Phones phone : phoneList) {


                    phone.setCreateBy(operName);
                    phone.setCreateTime(DateUtils.getNowDate());
                    phonesMapper.insertPhones(phone);
                }
            }catch (Exception e){
                e.printStackTrace();
            }



        return "";
    }

    @Override
    public int flushPhone(Phones phones) {
        String ipString = phones.getIps();
        String[] ips = ipString.split(",");
//        http://192.168.9.5:83/get/192.168.9.5
//        http://192.168.9.5:83/set_custom_devinfo/192.168.9.5/4cf202c60f230da66e13df14c1a58e77_1_T1001
        List<Phones> listPhone= phonesMapper.selectPhonesList(null);

//        获取ip下的机型列表



        for(int i=0;i<ips.length;i++){
          String url= "http://"+ips[i]+":83/get/"+ips[i];
            try {
               String result= httpClientHelper.sendGet(url,null);
                JSONObject map= JSON.parseObject(result);
                List<RongQiInfo> list= JSONArray.parseArray(map.getString("msg"),RongQiInfo.class);

                for (RongQiInfo rongQiInfo:list){
                    try {
                        String durl= "http://"+ips[i]+":83/set_custom_devinfo/"+ips[i]+"/"+rongQiInfo.getNames();
                      String jxjson;
                       if(listPhone.size()<i){
                           Random rand = new Random(); // 创建Random类的实例
                           int num = rand.nextInt(listPhone.size());
                           jxjson=listPhone.get(num).getPhoneJson();
                       }else {
                           jxjson=listPhone.get(i).getPhoneJson();
                       }
                        String dresult=httpClientHelper.doPostJSON(durl,jxjson);
                        JSONObject dmap= JSON.parseObject(dresult);
                        if (dmap.getIntValue("code")==200){
                            System.out.println("成功");
                        }
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }

                }



            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }


        return 0;
    }
}
